package com.situ.car.controller;

import com.situ.car.pojo.entity.Order;
import com.situ.car.pojo.query.OrderQuery;
import com.situ.car.pojo.vo.OrderVo;
import com.situ.car.service.ICarService;
import com.situ.car.service.IOrderService;
import com.situ.car.service.IUserService;
import com.situ.car.service.Impl.CarServiceImpl;
import com.situ.car.service.Impl.OrderServiceImpl;
import com.situ.car.service.Impl.UserServiceImpl;
import com.situ.car.util.MyBeanUtils;
import com.situ.car.util.PageInfo;
import com.situ.car.util.Result;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@WebServlet("/order")
public class OrderController extends BaseController{
    private IOrderService orderService = new OrderServiceImpl();
    private ICarService carService = new CarServiceImpl();
    private IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("OrderController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "addOrder" :
                add(req, resp);
                break;
            case "selectById" :
                selectById(req, resp);
                break;
            case "updateOrderStatus" :
                updateOrderStatus(req, resp);
                break;
            case "selectOrderNumber" :
                selectOrderNumber(req, resp);
                break;
            case "selectOrder" :
                selectOrder(req, resp);
                break;
        }
    }

    private void selectOrder(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderController.selectOrder");
        String userId = req.getParameter("userId");
        List<OrderVo> list = orderService.selectOrder(Integer.parseInt(userId));
        toJSON(resp, Result.ok(list));
    }

    private void selectOrderNumber(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderController.selectOrderNumber");
        List<Order> list = orderService.selectOrderNumber();
        toJSON(resp, Result.ok(list));
    }

    private void updateOrderStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderController.updateOrderStatus");
        String id = req.getParameter("id");
        String orderStatus = req.getParameter("orderStatus");
        orderService.updateOrderStatus(Integer.parseInt(id), Integer.parseInt(orderStatus));
        toJSON(resp, Result.ok("修改成功！"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderController.selectById");
        String id = req.getParameter("id");
        OrderVo orderVo = orderService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(orderVo));
    }



    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderController.add");
        String rentalStartDate = req.getParameter("rentalStartDate");
        String rentalEndDate = req.getParameter("rentalEndDate");
        System.out.println(rentalEndDate);

        if (ObjectUtils.isEmpty(rentalStartDate) || ObjectUtils.isEmpty(rentalEndDate)) {
            toJSON(resp, Result.error("请选择租赁日期！"));
        } else if (!ObjectUtils.isEmpty(rentalStartDate) && !ObjectUtils.isEmpty(rentalEndDate)){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime date1 = LocalDateTime.parse(rentalStartDate, formatter);
            LocalDateTime date2 = LocalDateTime.parse(rentalEndDate, formatter);
            if (date1.isAfter(date2) || date1.equals(date2)) {
                toJSON(resp, Result.error("请选择正确的租赁日期！"));
            } else if (date1.isBefore(date2)){
                Order order = MyBeanUtils.copyToBean(req, Order.class);
                orderService.add(order);
                String carId = req.getParameter("carId");
                carService.updateRentalStatus(Integer.parseInt(carId));
                toJSON(resp, Result.ok("下单成功！"));

            }
        }


    }




    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderController.selectByPage");
        OrderQuery orderQuery = MyBeanUtils.copyToBean(req, OrderQuery.class);
        PageInfo pageInfo = orderService.selectByPage(orderQuery);
        toJSON(resp, pageInfo);
    }
}
